
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>





#ifndef _XLOG_H_
#define _XLOG_H_
#include "config.h"

#include "log.h"
#include "type.h"
#include "lib.h"


#ifdef __cplusplus
extern "C" {
#endif

extern void logSetDebugLevel(int level);
extern void logSetFacility(int facility);
extern void logSetEnabled(bool enabled);
extern int logGetDebugLevel();
extern int logGetFacility();
extern unsigned int logGetMask();
extern bool logIsEnabled();
extern void logSetMask(unsigned int mask);

#define DEBUG_FACILITY_STDOUT 0
#define DEBUG_FACILITY_SYSLOG 1


#define TEST_MODULE_FIRST                TAC_MODULE_XX
#define TEST_MODULE_XX               1
#define TEST_MODULE_YY               2
#define TEST_MODULE_ZZ               3
#define TEST_MODULE_CONFIG               4

#define TEST_MODULE_LAST                 TAC_MODULE_ZZ


#define T_TRACE_ALL            0xffffffff   /* trace all modules/components */
#define T_TRACE_COMPONENT_MASK 0x000fffff   /* component mask */
#define T_TRACE_COMM_COMP_MASK 0x0000000f   /* common component mask */
#define T_TRACE_INDV_COMP_MASK 0x000ffff0   /* module specific component */
#define T_TRACE_MODULE_MASK    0xfff00000   /* module mask */
#define T_TRACE_INDV_COMP_SHIFT         4

/* define module bits here */
#define T_TRACE_XX          BIT(19 + TAC_MODULE_XX)  /*  */
#define T_TRACE_YY          BIT(19 + TAC_MODULE_YY) /* */
#define T_TRACE_ZZ          BIT(19 + TAC_MODULE_ZZ) /*  */


/* define common trace component for all modules */
#define T_TRACE_AA          BIT(0)       /*  */
#define T_TRACE_BB          BIT(1)       /*  */


#define T_TRACE_GET_MODULE(mask)   ((mask) & T_TRACE_MODULE_MASK)

#define T_TRACE_MAKE_MASK(module, comp) \
    (((module) & T_TRACE_MODULE_MASK) |  ((comp) & T_TRACE_COMPONENT_MASK))

/* trace level */
#define T_TRACE_EMERG            0
#define T_TRACE_ALERT            1
#define T_TRACE_CRIT             2
#define T_TRACE_ERR              3
#define T_TRACE_WARN             4
#define T_TRACE_NOTICE           5
#define T_TRACE_INFO             6
#define T_TRACE_DEBUG            7
#define T_TRACE_DEBUG2           8

#ifndef DEBUG_TIME
#define debug_wrapper(level, module, prefix, format, arg...) \
    { if (logGetDebugLevel() >= level && logGetMask()&((module & T_TRACE_COMPONENT_MASK))) {\
        printf(prefix " id(0x%08x) - %s(): " format "\n", module, __FUNCTION__, ##arg);\
    }}
#else
#define debug_wrapper(level, module, prefix, format, arg...) \
    { if (logGetDebugLevel() >= level && logGetMask()&((module & T_TRACE_COMPONENT_MASK))) {\
        printf(prefix "%s - %s(): line:  %d " format "\n",  get_time_string2(), __FUNCTION__, __LINE__, ##arg);\
    }}
#endif

#define debug(module, format, arg...) \
    debug_wrapper(T_TRACE_DEBUG, module, "[DEBUG]", format, ##arg)

#define error(module, format, arg...) \
    debug_wrapper(T_TRACE_ERR, module, "[ERROR]", format, ##arg)
    
#define warn(module, format, arg...) \
    debug_wrapper(T_TRACE_WARN, module, "[WARN ]", format, ##arg)

#define info(module, format, arg...) \
    debug_wrapper(T_TRACE_INFO, module, "[INFO ]", format, ##arg)

#define emflog(module, format, arg...) \
    debug_wrapper(T_TRACE_DEBUG, module, "[LOG  ]", format, ##arg)


#ifdef __cplusplus
}
#endif

#endif
